package com.dev.module_zqc_sleep.broadcast

import android.app.AlarmManager
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent

class StopAlarmReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        // 停止音频播放
        AlarmReceiver.mediaPlayer?.stop()
        AlarmReceiver.mediaPlayer?.release()
        AlarmReceiver.mediaPlayer = null

        // 取消闹钟
        val alarmId = intent?.getIntExtra("alarm_id", 0) ?: return
        val alarmManager = context?.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val cancelIntent = Intent(context, AlarmReceiver::class.java)
        val cancelPendingIntent = PendingIntent.getBroadcast(context, alarmId, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
        alarmManager.cancel(cancelPendingIntent)

        // 移除通知
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.cancel(alarmId)
    }
}
